﻿using System;
using UnityEngine;

// Version 1.2
// 增加世界模式调整
// Version 1.1
// 初始化UI结构
// 隐藏所有Page层
// 显示规定的层或默认显示第1个Page层

public class PageInit : MonoBehaviour
{
	[Tooltip("摄像机")]
	public Camera uiCamera;
	[Tooltip("默认启动Page")]
	public GameObject[] pages_show;
	[Tooltip("配置Page的世界模式,对象")]
	public GameObject[] pages_world_mode;
	[Header("参数浏览-自动生成")]
	[Tooltip("所有的Page")]
	public GameObject[] pages;


	void Start()
	{
		try
		{
			pages = GameObject.FindGameObjectsWithTag("Page");
			SetPage(pages);
		}
		catch (Exception e) { }
	}

	/// <summary>
	/// 设置默认显示也组合成ScreenSpaceCamera模式
	/// </summary>
	/// <param name="pages"></param>
	private void SetPage(GameObject[] pages)
	{
		for (int i = 0; i < pages.Length; i++)
		{
			//初始化Canvas
			Canvas canvas = pages[i].GetComponent<Canvas>();
			canvas.renderMode = RenderMode.ScreenSpaceCamera;
			canvas.worldCamera = uiCamera;
			pages[i].SetActive(false);
		}

		//判断是否需要调整RenderMode
		if (pages_world_mode.Length > 0)
		{
			for (int i = 0; i < pages_world_mode.Length; i++)
			{
				Canvas canvas = pages_world_mode[i].GetComponent<Canvas>();
				canvas.renderMode = RenderMode.WorldSpace;
			}
		}

		for (int i = 0; i < pages_show.Length; i++)
		{
			//初始化显示
			pages_show[i].SetActive(true);
		}

		//没有设置初始化显示的情况下
		if (pages_show.Length == 0)
		{
			pages[0].SetActive(true);
		}

		//屏蔽Tmp
		GameObject[] tmp = GameObject.FindGameObjectsWithTag("Tmp");
		for (int i = 0; i < tmp.Length; i++)
		{
			tmp[i].SetActive(false);
		}
	}
}
